module pack();

/*
    左流操作符: {<<N{data}} 反转元素顺序
    右流操作符: {>>N{data}} 原始元素顺序

    其中 data 是要处理的数据, N 是流操作的单位大小

    {<<1{data}} 每次处理1位, 不仅反转元素顺序，还反转每个元素内部的位
    {<<8{data}} 每次处理8位, 仅仅反转元素顺序, 但不反转每个元素内部的位
*/


// 在一个initial块内的多个begin-end块是顺序执行的
initial begin
    // Pack bytes into an int
    begin
        byte  b0 = 8'h01, b1 = 8'h23, b2 = 8'h45, b3 = 8'h67;
        int   packed_int = {>>{b0, b1, b2, b3}};
        $display("Pack bytes into an int: 0x%h", packed_int);
    end

    // Array of bytes to an int
    begin
        byte arr[4] = {8'h01, 8'h23, 8'h45, 8'h67};
        int   packed_int = {>>{arr}};
        $display("Array of bytes to an int: 0x%h", packed_int);
    end

    // Reverse the elements of a byte array and pack them into an int
    begin
        byte arr[4] = {8'h01, 8'h23, 8'h45, 8'h67};
        int  packed_int = {<<8{arr}};
        $display("Reverse the elements of a byte array and pack them into an int: 0x%h", packed_int);
    end

    //  Reverse the bits in a byte
    begin
        byte a = 8'b11100101;
        byte b = {<<1{a}};
        $display("Reverse the bits in a byte: 0b%b", b);
    end

    // Reverse the nibbles in a byte
    begin
        byte a = 8'b11100101;
        byte b = {<<4{a}};
        $display("Reverse the nibbles in a byte: 0b%b", b);
    end

    // Reverse the bits of an array and pack them into a shortint
    begin
        bit [7:0] arr[2] = '{ 8'h8C, 8'hA4 };
        shortint packed_shortint = {<<1{arr}};
        $display("Reverse the bits of an array and pack them into a shortint: 0x%h", packed_shortint);
    end
end

endmodule
